package sketch.seg;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import sketch.ink.SampledPoint;

public class CurveSegmenter {

	private List<SampledPoint> points = null;

	private List<SampledPoint> corners = null;

	private List<List<SampledPoint>> fragments = null;

//	private List<Shape> fragments = null;

//	private PrimApproximator pa = new PrimApproximator();

	public List<List<SampledPoint>> getFragments() {
		if (fragments == null)
			segment();

		return fragments;
	}

	public void segment() {
		fragments = new ArrayList<List<SampledPoint>>();

		Iterator<SampledPoint> itOfPs = points.iterator();
		for (Iterator<SampledPoint> it = corners.iterator(); it.hasNext(); ) {
			SampledPoint corner = it.next();

			List<SampledPoint> subps = new ArrayList<SampledPoint>();
			for (; itOfPs.hasNext(); ) {
				SampledPoint cand = itOfPs.next();
				subps.add(cand);
				if (cand.equals(corner)) {
					break;
				}
			}

			if (subps.size() >= 2) {
				fragments.add(subps);
			}
		}

		List<SampledPoint> rest = new ArrayList<SampledPoint>();
		for (; itOfPs.hasNext(); ) {
			rest.add(itOfPs.next());
		}

		if (rest.size() >= 2) {
			fragments.add(rest);
		}
	}

	public void setInputPoints(List<SampledPoint> ps, List<SampledPoint> cs) {
		points = ps;
		corners = cs;

		fragments = null;
	}

}
